package main

import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	"grpc_starter/grpc_stream/pb"
)

func main() {
	conn, _ := grpc.Dial("127.0.0.1:5001", grpc.WithInsecure())
	defer conn.Close()
	client := pb.NewGreetClient(conn)

	// server stream mode
	getStreamData, _ := client.GetStream(context.TODO(), &pb.StreamReqData{Message: "hello world", Gender: pb.Gender_FEMALE, Map: map[string]string{
		"name": "fangtongle",
		"age":  "30",
	}})

	for {
		resData, _ := getStreamData.Recv()
		if resData == nil {
			continue
		}
		fmt.Println("got data from getStreamData: ", resData.Data)
	}

	// client stream mode
	//stream, _ := client.PutStream(context.TODO())
	//
	//count := 0
	//for {
	//	count++
	//	_ = stream.Send(&pb.StreamReqData{
	//		Message: fmt.Sprintf("%d %s %v", count, "hello golang", time.Now().Unix()),
	//	})
	//	time.Sleep(time.Second)
	//	if count >= 10 {
	//		break
	//	}
	//}

	// Bi-directional stream of server and client
	//var wg sync.WaitGroup
	//
	//stream, _ := client.AllStream(context.TODO())
	//
	//wg.Add(2)
	//
	//go func() {
	//	defer wg.Done()
	//	for {
	//		recData, _ := stream.Recv()
	//		fmt.Println("got server data: ", recData.Data)
	//		time.Sleep(time.Second)
	//	}
	//}()
	//
	//go func() {
	//	defer wg.Done()
	//	for {
	//		_ = stream.Send(&pb.StreamReqData{Message: "i'm client send message"})
	//		time.Sleep(time.Second)
	//	}
	//}()
	//
	//wg.Wait()
}
